﻿@{
    Layout = null;
}
<html>
    <head>
    <meta name="viewport" content="width=device-width" />
    <title>Upload</title>
    <link href="/layui/css/layui.css" rel="stylesheet" />
    <link href="/layui/extend/icon/iconfont.css" rel="stylesheet" />
    </head>
<body>
    <div class="layui-upload">
        <button type="button" class="layui-btn" id="upload">上传图片</button>
        <div class="layui-upload-list">
            <img class="layui-upload-img" id="preview">
            <p id="filename"></p>
        </div>
    </div>
    <script src="/layui/layui.all.js"></script>
<script>
        layui.use('upload', function () {
            var upload = layui.upload;

            upload.render({
                elem: '#upload',
                url: '/admin/product/upload/',
                done: function (res) {
                    // 上传成功后的回调函数
                    if (res.code == 0) {
                        // 上传成功
                        $('#preview').attr('src', res.data.src);
                        $('#filename').html(res.data.filename);

                        // 将上传的图片数据保存到数据库中
                        $.post('/admin/product/save/', {
                            src: res.data.src,
                            filename: res.data.filename
                        }, function (data) {
                            // 保存成功后的回调函数
                            if (data.code == 0) {
                                // 保存成功
                                layer.msg('保存成功');
                            } else {
                                // 保存失败
                                layer.msg('保存失败');
                            }
                        });
                    } else {
                        // 上传失败
                        layer.msg('上传失败');
                    }
                }
            });
        });

    </script>
    </body>
</html>
